package cn.xiaosiyuan.util.dataStructure.objectType;

import cn.xiaosiyuan.util.interfaces.IObject;

import java.io.*;

public class ObjectUtil implements IObject {
    public <T> T deepClone(T obj){
        T clonedObj = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try{
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ois = new ObjectInputStream(bais);
            clonedObj = (T) ois.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                oos.close();
                ois.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return clonedObj;
    }
}
